package com.lab.component.list.simple;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.lab.adapter.IAdapterData;
import com.lab.component.list.adapter.CTRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ak on 4/5/16.
 */
public abstract class CTSimpleRecyclerAdapter<T extends IAdapterData> extends CTRecyclerAdapter<CTSimpleViewHolder> implements SimpleAdapterInterface<T> {

    protected List<T> mDataList;
    protected LayoutInflater mInflater;
    private Context mContext;

    public CTSimpleRecyclerAdapter(Context ctx) {
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
    }

    public CTSimpleRecyclerAdapter(Context ctx, List<T> list) {
        mDataList = (list != null) ? list : new ArrayList<T>();
        mContext = ctx;
        mInflater = LayoutInflater.from(ctx);
    }


    @Override
    public CTSimpleViewHolder newViewHolder(ViewGroup parent, int viewType) {
        final CTSimpleViewHolder holder = new CTSimpleViewHolder(
                mInflater.inflate(getItemLayoutId(viewType), parent, false));
        onNewViewHolder(holder, parent, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(CTSimpleViewHolder holder, int position) {
        bindData(holder, position, mDataList.get(position));
    }

    public int getListSize(List list) {
        return list == null ? 0 : list.size();
    }

    public void setDataList(List<T> dataList) {
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        int originSize = getListSize(mDataList);
        mDataList.clear();
        mDataList.addAll(dataList);
        int nowSize = getListSize(mDataList);
        if (originSize <= 0) {
            notifyItemRangeInserted(0, nowSize);
        } else {
            if (originSize < nowSize) {
                notifyItemRangeChanged(0, originSize);
                notifyItemRangeInserted(originSize, nowSize - originSize);
            } else if (originSize == nowSize) {
                notifyItemRangeChanged(0, nowSize);
            } else {
                notifyItemRangeChanged(0, nowSize);
                notifyItemRangeRemoved(nowSize, originSize - nowSize);
            }
        }
    }

    public void appendDataList(List<T> dataList) {
        if (this.mDataList == null || this.mDataList.isEmpty()) {
            setDataList(dataList);
        } else {
            int size = this.mDataList.size();
            this.mDataList.addAll(dataList);
            notifyItemRangeInserted(size, getListSize(dataList));
        }
    }

    public void removeMessageMode(T data) {
        int index = mDataList.indexOf(data);
        if (index >= 0) {
            mDataList.remove(data);
            notifyItemRemoved(index);
        }
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    abstract public int getItemLayoutId(int viewType);

    abstract public void onNewViewHolder(CTSimpleViewHolder viewHolder, ViewGroup parent, int viewType);

    abstract public void bindData(CTSimpleViewHolder holder, int position, T item);


}
